class MyPlane extends Base {
	constructor() {
		super(myPlaneImg)
		this.name = 'myPlane';
		//让页面充分准备好
		$(document).ready(_ => {
			let timer = setTimeout(_ => {
				clearTimeout(timer)
				this.bindMouse()
				//发射子弹
				this.sendBullet()
				//更新游戏状态为进行中
				Base.status = 1;
			})
		})
	}
	//飞机跟着鼠标动
	bindMouse() {
		const { clientWidth, clientHeight, offsetLeft, offsetTop } = this.dom;
		this.width = clientWidth;
		this.height = clientHeight;
		this.left = offsetLeft;
		this.top = offsetTop;
		$('body').on('mousemove', e => {
			const { clientX, clientY } = e;
			this.left = clientX - clientWidth / 2;
			this.top = clientY - clientHeight / 2;
			$(this.dom).css('left', this.left + 'px');
			$(this.dom).css('top', this.top + 'px');
		})
	}
	//发射子弹
	sendBullet() {
		setInterval(_ => {
			//只有游戏进行中才需要发射子弹，子弹发射器不需要清除
			if (Base.status === 1) new Bullet(this, Base.diAll);
		}, 200)
	}
	//移除我方飞机和敌机
	remove(di) {
		//改变我方飞机的效果
		this.changeSrc('boomSrc');
		let timer = setTimeout(_ => {
			clearTimeout(timer)
			//隐藏我方战机，等待复活，不能移除
			$(this.dom).hide()
			//移除敌机
			di.remove(true)
			//更新游戏状态为结束游戏
			Base.status = 2;
			//弹出对话框
			this.duihua()
		}, 200)
	}
	//弹出对话框
	duihua() {
		$('#duihua').css('display', 'inline-block')
			.click(e => {
				switch (e.target.innerText) {
					case '立即复活':
						//恢复满血状态
						this.changeSrc('baseSrc');
						this.blood = this.imgs.blood;
						//更新游戏状态为进行中
						Base.status = 1;
						//显示我方战机
						$(this.dom).show();
						//找到弹框父级并隐藏
						$(e.target).parent().hide();
						break;
					case '重新开始':
						//存入榜单
						this.store()
						location.reload()

						break;
					case '返回主页':
						//存入榜单
						this.store()
						location.href = './start.html';
						break;
				}
			});
	}
	//存入榜单
	store() {
		let destroyDi = $('#box>p:nth-child(1)>span').text();
		let score = $('#box>p:nth-child(2)>span').text();
		//创造当前分数记录
		const record = { score, destroyDi, time: new Date().toLocaleDateString() };
		//获取历史记录
		let data = [];
		if (localStorage.bang) {
			data = JSON.parse(localStorage.bang);
			//对记录插入进入
			for (let i = 0; i < data.length; i++) {
				const item = data[i];
				if (Number(item.score) < Number(score)) {
					//插入当前记录
					data.splice(i, 0, record)
					//终止循环
					break;
				}
			}
		} else {
			data.push(record)
		}
		//存入记录，榜单只记录6个最高分
		localStorage.bang = JSON.stringify(data.slice(0, 6));
	}
}
